package com.zhangpan.realtime.common.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zhangpan.realtime.common.constant.Constant;
import io.lettuce.core.RedisClient;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.async.RedisAsyncCommands;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author zhangpan
 */
public class RedisUtil {

    private final static JedisPool pool;

    static {

        GenericObjectPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(300);
        config.setMaxIdle(10);
        config.setMinIdle(2);

        config.setTestOnCreate(true);
        config.setTestOnBorrow(true);
        config.setTestOnReturn(true);
        config.setMaxWaitMillis(10 * 1000);
        pool = new JedisPool(config,"hdp134",6379);
    }

    public static Jedis getJedis() {

        Jedis jedis = pool.getResource();
        jedis.select(4);

        return jedis;
    }

    public static String getKey(String tableName,String id) {

        return tableName + ":" + id;
    }

    public static JSONObject readDim(Jedis jedis, String tableName, String id) {
        String key = getKey(tableName,id);
        String jsonStr = jedis.get(key);
        if (jsonStr != null) {
            return JSON.parseObject(jsonStr);
        }
        return null;
    }

    public static void writeDim(Jedis jedis,String tableName,String id,JSONObject dim) {

        jedis.setex(getKey(tableName,id), Constant.TWO_DAY_SECONDS,dim.toJSONString());
    }

    public static void closeJedis(Jedis jedis) {
        if (jedis != null) {
            jedis.close();
        }
    }

    public static StatefulRedisConnection<String,String> getRedisAsyncConnection() {
        RedisClient redisClient = RedisClient.create("redis://hdp134:6379/4");
        return redisClient.connect();
    }

    public static void closeRedisAsyncConnection(StatefulRedisConnection<String,String> redisAsyncConn) {

        if (redisAsyncConn != null) {
            redisAsyncConn.close();
        }
    }

    public static JSONObject readDimAsync(StatefulRedisConnection<String,String> redisConnection,
                                          String tableName,
                                          String id) {
        RedisAsyncCommands<String,String> asyncCommands = redisConnection.async();
        String key = getKey(tableName,id);
        try {
            String json = asyncCommands.get(key).get();
            if (json != null) {
                return JSON.parseObject(json);
            }
        }catch (Exception e) {
            throw new RuntimeException(e);
        }

        return null;
    }

    public static boolean writeDimAsync(StatefulRedisConnection<String,String> redisConnection,
                                        String tableName,
                                        String id,
                                        JSONObject dim) {

        //1. 得到异步命令
        RedisAsyncCommands<String,String> asyncCommands = redisConnection.async();

        String key = getKey(tableName,id);

        //2. 写入到string中 顺便还设置的ttl
        asyncCommands.setex(key,Constant.TWO_DAY_SECONDS, dim.toJSONString());
        return false;
    }

    public static void main(String[] args) {
        JSONObject dim = new JSONObject();
        dim.put("test",11111);
        System.out.println(writeDimAsync(getRedisAsyncConnection(),"dim_sku_info","1",dim));
    }

}
